import { loginUser, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { ElLoadingComponent } from 'element-ui/types/loading'
// import { resetRouter } from '@/router'

// const getDefaultState = () => {
//   return
// }

const state = {
  token: getToken() || '',
  userInfo: {}

}

const mutations = {

  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  removeToken(state) {
    state.token = ''
    removeToken()
  },
  updateInfo(state, info) {
    state.userInfo = info
  }

}

const actions = {
  // 登录 存token
  async login(context, formData) {
    const res = await loginUser(formData)
    // console.log(res)
    context.commit('setToken', res.data.token)
  },
  // 获取用户信息 存到vuex
  async getInfo(context) {
    const res = await getInfo()
    // console.log(res.data.userInfo)
    context.commit('updateInfo', res.data.userInfo)
  },
  // token 失效
  logout(context) {
    context.commit('removeToken')
    context.commit('updateInfo', {})
  },

  // remove token
  resetToken(context) {
    context.commit('removeToken')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

